package com.example.cafe.work.controller;


import com.example.cafe.work.dto.Result;
import com.example.cafe.work.model.User;
import com.example.cafe.work.service.UserService;
import com.example.cafe.work.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-09-16
 */
@RestController
@RequestMapping("/user")
public class UserController {
    //添加用户时，添加的不仅仅是user表，还有userinfo表
    //查询用户时，两张表查询
    //用户的登录注册
    @Autowired
    UserService userService;

    @RequestMapping("/add")
    public Result addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @DeleteMapping("/del")
    public Result delCoffee(@RequestBody User user) {
        //拿到id直接删，可以将请求体简写一个参数传
        boolean b = userService.removeById(user.getUserId());
        return Result.ok(b);
    }

    @PutMapping("/update")
    public Result updateCoffee(@RequestBody User user) {
        //修改反正拿id，其他不管直接调plus的方法
       return Result.ok(userService.updateById(user));
    }

    @RequestMapping("/selectAll")
    //分页查询数据，带page和size，可以把size优化写死
    public Result selectAll(@RequestParam("page") int page,
                            @RequestParam("size") int size,
                            @RequestParam("user_name") String name){
        return userService.select(name,page,size);
    }


    @RequestMapping("/login")
    public Result login(@RequestParam("name")String userName,@RequestParam("password")String password){
        //注册没写，上面有添加，但添加是要带token的
       return userService.login(userName,password);
    }


    @RequestMapping("/me")
    public Result getUser(){
        return Result.ok(UserHolder.getUser());
    }
}

